import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
const api = {}
const electronAPIWithIpc = {
  ...electronAPI,
  // 添加文件预览API
  openFilePreview: (fileData: any) => ipcRenderer.invoke('open-file-preview', fileData),
  // 添加通知API
  notification: {
    // 显示系统通知
    show: (config: any, id?: string) => ipcRenderer.invoke('show-notification', config, id),
    // 关闭通知
    close: (id: string) => ipcRenderer.invoke('close-notification', id),
    // 获取活动通知
    getActive: () => ipcRenderer.invoke('get-active-notifications'),
    // 监听通知点击事件
    onClicked: (callback: (data: any) => void) => {
      const wrappedCallback = (_: any, data: any) => callback(data)
      ipcRenderer.on('notification-clicked', wrappedCallback)
      return () => ipcRenderer.removeListener('notification-clicked', wrappedCallback)
    }
  },
  ipcRenderer: {
    send: ipcRenderer.send.bind(ipcRenderer),
    on: ipcRenderer.on.bind(ipcRenderer),
    once: ipcRenderer.once.bind(ipcRenderer),
    removeListener: ipcRenderer.removeListener.bind(ipcRenderer),
    removeAllListeners: ipcRenderer.removeAllListeners.bind(ipcRenderer),
    invoke: ipcRenderer.invoke.bind(ipcRenderer)
  }
}
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPIWithIpc)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error('Failed to expose Electron APIs:', error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPIWithIpc
  // @ts-ignore (define in dts)
  window.api = api
}
